package com.huawei.hmsdemo10086.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.huawei.hms.hwid.I;
import com.huawei.hmsdemo10086.Adapter.GoodsAdapter;
import com.huawei.hmsdemo10086.Adapter.VideoAdapter;
import com.huawei.hmsdemo10086.POJO.Goods;
import com.huawei.hmsdemo10086.POJO.Result;
import com.huawei.hmsdemo10086.R;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class GoodsActivity extends AppCompatActivity {

    private Result<List<Goods>>  result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods);

        TextView mTitleTextView = (TextView) this.findViewById(R.id.text_title);
        mTitleTextView.setText("商品列表");
        Button back = (Button)this.findViewById(R.id.button_backward);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GoodsActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

        getGoods();

        while (result==null){
            Log.d("wait", "wait goods init");
        }

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.goods_recycle_view);
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        GoodsAdapter adapter = new GoodsAdapter(this,result.getData());

        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickLitener(new VideoAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(GoodsActivity.this,GoodsDetailsActivity.class);
                intent.putExtra("goods",result.getData().get(position));
                startActivity(intent);
            }

            @Override
            public void onCollection(View view, int position) {

            }
        });
    }

    private void getGoods()
    {
        String url = "http://10.0.2.2:8443/api/listGoods";
        OkHttpClient mOkHttpClient = new OkHttpClient();

        Request request = new Request.Builder().url(url).get().build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                Gson gson = new Gson();
                String rep = response.body().string();
                System.out.println(rep);

                Type type = new TypeToken<Result<List<Goods>>>(){}.getType();
                result = gson.fromJson(rep,type);
                for(int i = 0 ; i < result.getData().size() ; i++)
                {
                    System.out.println(result.getData().get(i).toString());
                }
            }
        });
    }
}